/**
 * Created by admin on 2017/7/21.
 */
layui.use(['form', 'element','laydate','upload'], function(){
    var $ = layui.jquery, form = layui.form();
    var element = layui.element();

    form.on('select(cityState)', function(data){
        var cityId = $('#cityCode').val();
        if (cityId == -1) {
            var select = '<select name="districtCode" id="districtCode" class="state" lay-filter="districtState" >';
            select += "<option value=\"-1\">请选择</option>";
            select += "</select>";
            $('#district').html(select);
            form.render('select');

            var select1 = '<select name="areaCode" id="areaCode" class="state" lay-filter="areaState" >';
            select1 += "<option value=\"-1\">请选择</option>";
            select1 += "</select>";
            $('#area').html(select1);
            form.render('select');
            $("#CityName").val("");
            $("#districtName").val("");
            $("#areaName").val("");
        }else {
            var select = '<select name="districtCode" id="districtCode" class="state" lay-filter="districtState" >';
            $.getJSON('/newHouse/getRegions?id='+cityId, function(json){
                select += "<option value=\"-1\" >请选择</option>";
                var list = json.data;
                $(list).each(function(){
                    select += "<option value=\"" + this.code + "\"";
                    select += ">" + this.name + "</option>";
                });
                select += "</select>";
                $('#district').html(select);
                form.render('select');

                var select1 = '<select name="areaCode" id="areaCode" class="state" lay-filter="areaState" >';
                select1 += "<option value=\"-1\">请选择</option>";
                select1 += "</select>";
                $('#area').html(select1);
                form.render('select');
            });
        }
        if ($("#cityCode").val() != -1) {
            $("#CityName").val($("#cityCode").find("option:selected").text());
        }else {
            $("#CityName").val("");
        }
    });

    form.on('select(districtState)', function(data){
        var districtId = $('#districtCode').val();
        var select = '<select name="areaCode" id="areaCode" class="state" lay-filter="areaState" >';
        $.getJSON('/newHouse/getBoards?id='+districtId, function(json){
            select += "<option value=\"-1\" >请选择</option>";
            var list = json.data;
            $(list).each(function(){
                select += "<option value=\"" + this.id + "\"";
                select += ">" + this.name + "</option>";
            });
            select += "</select>";
            $('#area').html(select);
            form.render('select');
        });
        if ($("#districtCode").val() != -1) {
            $("#districtName").val($("#districtCode").find("option:selected").text());
            $("#areaName").val("");
        } else {
            $("#districtName").val("");
            $("#areaName").val("");
        }
    });

    form.on('select(areaState)', function(data){
        if ($("#areaCode").val() != -1) {
            $("#areaName").val($("#areaCode").find("option:selected").text());
        } else {
            $("#areaName").val("");
        }
    });

    form.verify({
        testNumber:  function(value){
            if(value.length > 0){
                var reg = new RegExp("^[1-9][0-9]*$");
                if(!reg.test(value)){
                    return '请输入数字!';
                }
            }
        },
        testFoltNumber: function (value) {
            if(value.length > 0){
                if (value.length>6){
                    return "请输入5位数之内数字!";
                }
                var reg = new RegExp("^[0-9]+([.]{1}[0-9]+){0,1}$");
                if(!reg.test(value)){
                    return '请输入数字!';
                }
            }
        }
    });
    //多选框监听
    form.on('checkbox(f_estate)', function(data){
        $('#estateType').val($('input[name=estateTypeTemp]:checked').map(
            function () {
                return this.value
            }
        ).get().join(','));
    });
    //多选框监听
    form.on('checkbox(f_building)', function(data){
        $('#buildingType').val($('input[name=buildingTypeTemp]:checked').map(
            function () {
                return this.value
            }
        ).get().join(','));
    });

    //多选框监听
    form.on('checkbox(f_metroLine)', function(data){
        $('#metroLine').val($('input[name=metroLineTemp]:checked').map(
            function () {
                return this.value
            }
        ).get().join(','));
    });

    //监听提交
    form.on('submit(baseInfo_submit)', function(data){
        if ( $("input[name='name']").val().length>20){
            layer.msg("楼盘名不能超过20字!");
            return false;
        }
        if ( $("input[name='aliasName']").val().length>20){
            layer.msg("楼盘别名不能超过20字!");
            return false;
        }
        if ( $("#cityCode").val()==undefined ||$("#cityCode").val()==-1 ){
            layer.msg("请选择城市!");
            return false;
        }

        if ( $("#districtCode").val()==undefined ||$("#districtCode").val()==-1){
            layer.msg("请选择区域!");
            return false;
        }

        if ( $("#areaCode").val()==undefined ||$("#areaCode").val()==-1){
            layer.msg("请选择板块!");
            return false;
        }
        if ( $("input[name='address']").val().length>70){
            layer.msg("楼盘地址不能超过70字!");
            return false;
        }
        if ( $("input[name='saleAddress']").val().length>140){
            layer.msg("楼盘售楼地址不能超过140字!");
            return false;
        }
        if ( $("input[name='salePhone']").val().length>15){
            layer.msg("销售电话不能超过15字!");
            return false;
        }
        if ( $("input[name='avgPrice']").val().length>8){
            layer.msg("均价不能超过8位数字!");
            return false;
        }

        if ($("#developerId").val()==null||  $("#developerId").val()==undefined||  $("#developerId").val()==""){
            layer.msg("请选择开发商!");
            return false;
        }

        if ($("#mangerCompany").val()!=null &&  $("#mangerCompany").val()!=undefined &&  $("#mangerCompany").val()!=""){
            if ($("#mangerId").val()==null||  $("#mangerId").val()==undefined||  $("#mangerId").val()==""){
                layer.msg("请选择物业公司!");
                return false;
            }
        }

        if ( $("input[name='saleStatus']:checked").val()==undefined){
            layer.msg("请选择销售状况!");
            return false;
        }
        if ($("input[name='metroLineTemp']:checked").val()==undefined){
            layer.msg("请选择地铁线!");
            return false;
        }
        if ( $("input[name='estateTypeTemp']:checked").val()==undefined){
            layer.msg("请选择物类型!");
            return false;
        }
        if ($("input[name='buildingTypeTemp']:checked").val()==undefined){
            layer.msg("请选择建筑类型!");
            return false;
        }

        if ( $("input[name='permitYears']").val().length>3){
            layer.msg("产权年限不能超过3位数字");
            return false;
        }
        if ( $("input[name='landSpace']").val().length>8){
            layer.msg("占地面积不能超过8位数字");
            return false;
        }
        if ( $("input[name='householdNum']").val().length>5){
            layer.msg("规划户数不能超过5位数字");
            return false;
        }
        if ( $("input[name='buildingAcreage']").val().length>8){
            layer.msg("建筑面积不能超过8位数字");
            return false;
        }
        if ( $("input[name='parkingNum']").val().length>20){
            layer.msg("停车位不能超过20字");
            return false;
        }

        if ( $("input[name='presale']").val().length>50){
            layer.msg("预售许可证不能超过50字!");
            return false;
        }
        addOrUpdate(data.field);
        return false;
    });

    element.on('tab(newhousetab)', function(){
        var tab = this.getAttribute("lay-id");
        if (tab == 'lay') {
            queryLayoutImage();
        }
        if (tab == 'local') {
            addMapMark();
        }
        if (tab == 'image') {
            queryHouseImage();
        }
        if (tab == 'business') {
            queryBusInfo();
        }
        if (tab == 'dynamic') {
            queryDynamicList();
        }
    });

    layui.upload({
        url: '/upload/imageNew',
        elem: '#layout_imag',
        method: 'post',
        type:'multipart/form-data'
        , success: function (data) {
            if(data.success == true){
                layout_img_url.src = data.data.url;
                $("#layoutKey").val(data.data.key);
            }else{
                layer.open({title: '错误',content: data.message});
            }
        }
    });

    layui.upload({
        url: '/upload/imageNew',
        elem: '#newHouse_imag',
        method: 'post',
        type:'multipart/form-data'
        , success: function (data) {
            if(data.success == true){
                newHouse_img_url.src = data.data.url;
                $("#newHouseKey").val(data.data.key);
            }else{
                layer.open({title: '错误',content: data.message});
            }
        }
    });
});

function addTag(value){
    var temp = $("#tags").val();
    if (temp != undefined && temp != '') {
        if (temp.indexOf(value)<0){
            temp = temp + "," + value;
            $("#tags").val(temp);
        }else {
            var newTags = '';
            var arr = temp.split(',');
            var arrOther = new Array();
            for(var i in arr){
                if(arr[i]!=value&&arr[i]!=''){
                    arrOther[i]=arr[i];
                }
            }
            $("#tags").val($.grep(arrOther, function(n) {return $.trim(n).length > 0;}).join(","));
        }
    }
    if (temp == ''||temp == undefined){
        $("#tags").val(value);
    }
    return false;
}

function addOrUpdate(obj) {
    $.ajax({
        type: "POST",
        url: "/newHouse/add",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: obj,
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                $("#id").val(data.data);
                closeTabWithMsgConfirm();
//                    tags.location="../house/index";
            } else {
                layer.open({
                    title: '错误'
                    ,content: data.message
                });
            }
        }
    });
}

function addDesc() {
    var url = "";
    var id = $('#id').val();
    if(id == "-1"){
        layer.msg("请先保存楼盘基础信息!");
        return false;
    }
    if($("#desc").val()!=''||$("#desc").val()!=undefined){
        if ($("#desc").val().length > 500) {
            layer.msg("描述信息请在500字内!");
            return false;
        }
    }
    $.ajax({
        type: "POST",
        url: "/newHouse/addDesc",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:$("#id").val(),
            description:$("#desc").val(),
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                layer.msg("保存成功!");
            } else {
                layer.open({
                    title: '错误'
                    ,content: data.message
                });
            }
        }
    });
}
/**
 * 添加经纬度
 * */
function addlglt() {
    var url = "";
    var id = $('#id').val();
    if(id == "-1"){
        layer.msg("请先保存楼盘基础信息!");
        return false;
    }
    $.ajax({
        type: "POST",
        url: "/newHouse/addlgla",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:$("#id").val(),
            longitude:$("#areaLongitude").val(),
            latitude:$("#areaLatitude").val()
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                layer.msg("保存成功!");
            } else {
                layer.open({
                    title: '错误'
                    ,content: data.message
                });
            }
        }
    });
}

/**户型图*/
function queryLayoutImage() {
    var id = $("#id").val();
    if(id != "-1"){
        var url = "/newHouse/getLayoutList";
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                estateId:id
            },
            dataType: 'json',
            success: function (data) {
                if (data.success == true) {
                    var list = data.data.list;
                    if (list != null) {
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            var obj = list[i];
                            html += "<tr>"
                                + " <td>" + obj.id + "</td>"
                                + " <td>" + obj.name + "</td>"
                                + "<td>" + obj.saleStateString + "</td>"
                                + "<td>" + obj.layoutString + "</td>"
                                + "<td>" + obj.buildingArea + "</td>"
                                + "<td><img src='" + obj.imgUrl + "' width='100px' height='50px'/></td>"
                                + "<td>"
                                + "<button onclick=toLayoutUpdate("+obj.id+") class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>编辑</button>"
                                + "<button onclick=toSubDelete("+obj.id+",1) class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>删除</button>"
                                + "</td>"
                                + "</tr>";
                        }
                        $("#layout_value").html(html);
                    }
                } else {
                    layer.open({
                        title: '错误'
                        ,content: data.message
                    });
                }
            }
        });
    }

}

/**
 *  增加户型图
 * */
function addLayout(){
    var estateId=$("#id").val();
    if(estateId == -1){
        layer.msg("请保存新房基础数据!");
        return false;
    }
    layer.open({
        type: 1,
        title:'户型图',
        content:$('#layout_hide'),
        area: ['550px', '630px'],
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes:function(){
            if($("#layoutKey").val() == null || $("#layoutKey").val() == "" || $("#layoutKey").val() == undefined){
                alert("请上传图片!");
                return;
            }
            if($("#layoutName").val() == null || $("#layoutName").val() == "" || $("#layoutName").val() == undefined){
                alert("名称不能为空!");
                return;
            }else {
                if ($("#layoutName").val().length > 5) {
                    alert("名称超过5个字,请修改!");
                    return false;
                }
            }
            if($("#layoutArea").val() == null || $("#layoutArea").val() == "" || $("#layoutArea").val() == undefined){
                alert("面积不能为空!");
                return;
            }else {
                if ($("#layoutArea").val().length > 5) {
                    alert("面积超过5位数字,请修改!");
                    return false;
                }
                var reg = new RegExp("^[0-9]*$");
                if(!reg.test($("#layoutArea").val())){
                    alert("面积请输入数字！");
                    return false;
                }
            }
            if($("#toilet").val() == null || $("#toilet").val() == "" || $("#toilet").val() == undefined
                || $("#bedroom").val() == "" || $("#bedroom").val() == undefined
                || $("#livingroom").val() == "" || $("#livingroom").val() == undefined){
                alert("户型不能为空");
                return;
            }else {
                if ($("#toilet").val().length > 2||$("#bedroom").val().length >2 ||$("#livingroom").val().length >2 ) {
                    alert("户型超过2位数字,请修改!");
                    return false;
                }
                var reg = new RegExp("^[0-9]*$");
                if(!reg.test($("#toilet").val())||!reg.test($("#bedroom").val())||!reg.test($("#livingroom").val())){
                    alert("户型请输入数字！");;
                    return false;
                }
            }
            if($("#layout_desc").val() != null || $("#layout_desc").val() != "" || $("#layout_desc").val() != undefined){
                if ($("#layout_desc").val().length > 200) {
                    alert("描述信息超过200个字,请修改!");
                    return false;
                }
            }

            $.ajax({
                type: "POST",
                url: "/newHouse/addLayout",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                data: {
                    id:$("#layoutId").val(),
                    estateId: estateId,
                    name:$("#layoutName").val(),
                    saleState:$("input[name='layoutSale']:checked").val(),
                    bedroom:$("#bedroom").val(),
                    livingroom:$("#livingroom").val(),
                    toilet:$("#toilet").val(),
                    imgKey:$("#layoutKey").val(),
                    buildingArea:$("#layoutArea").val(),
                    description: $("#layout_desc").val()
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success == true) {
                        queryLayoutImage();
                        $("input[id='layoutHideRest']").click();
                        layout_img_url.src = "";
                        $("#layoutKey").val("");
                        $("#layoutId").val("");
                        layer.closeAll();
                        layer.msg('操作成功!');
                    } else {
                        layer.msg({title: '错误',content: data.message});
                    }
                }
            });
        },
        btn2:function () {
            $("input[id='layoutHideRest']").click();
            layout_img_url.src = "";
            $("#layoutId").val("");
            $("#layoutKey").val("");
        },cancel: function(){
            $("input[id='layoutHideRest']").click();
            layout_img_url.src = "";
            $("#layoutId").val("");
            $("#layoutKey").val("");
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
}


function toLayoutUpdate(id) {
    $.ajax({
        type: "POST",
        url: "/newHouse/getLayoutById",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:id,
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                var obj = data.data;
                layout_img_url.src = obj.imgUrl;
                $("#layoutId").val(obj.id);
                $("#layoutName").val(obj.name);
                $("#bedroom").val(obj.bedroom);
                $("#livingroom").val(obj.livingroom);
                $("#toilet").val(obj.toilet);
                $("#layoutKey").val(obj.imgKey);
                $("#layoutArea").val(obj.buildingArea);
                $("#layout_desc").val(obj.description);

                $(":radio[name='layoutSale'][value='" + obj.saleState + "']").prop("checked", "checked");
                layui.use(['form'], function(){
                    form = layui.form();
                    form.render('radio');
                });
                addLayout();
            } else {
                layer.msg({title: '错误',content: data.message});
            }
        }
    });
}

/**
 * 查询房源图片
 */
function queryHouseImage() {
    var id = $("#id").val();
    if(id != "-1"){
        var url = "/newHouse/getImageList";
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                estateId:id,
                imgType:$("#imgTypeSearch").val()
            },
            dataType: 'json',
            success: function (data) {
                if (data.success == true) {
                    var list = data.data;
                    if (list != null) {
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            var obj = list[i];
                            html += "<tr>"
                                + " <td>" + obj.id + "</td>"
                                + " <td>" + obj.imgTypeString + "</td>"
                                + "<td>" + obj.recommendString + "</td>"
                                + "<td><img src='" + obj.imgUrl + "' width='100px' height='50px'/></td>"
                                + "<td>"
                                + "<button onclick=toNHImageUpdate("+obj.id+") class='layui-btn layui-btn-mini'>编辑</button>"
                                + "<button onclick=toSubDelete("+obj.id+",2) class='layui-btn layui-btn-mini'>删除</button>";
                                if (obj.imgType == 1) {
                                    if (obj.recommend == 1) {
                                        html += "<button onclick=toRecommend(" + obj.id + ",0) class='layui-btn layui-btn-mini'>取消首页图</button>"
                                            + "</td>"
                                            + "</tr>";
                                    } else {
                                        html += "<button onclick=toRecommend(" + obj.id + ",1) class='layui-btn layui-btn-mini'>首页图</button>"
                                            + "</td>"
                                            + "</tr>";
                                    }

                                } else {
                                    html += "</td></tr>";
                                }

                        }
                        $("#image_value").html(html);
                    }
                } else {
                    layer.open({
                        title: '错误'
                        ,content: data.message
                    });
                }
            }
        });
    }
}

/**
 *  增加图
 * */
function addNewHouseImage(){
    var estateId=$("#id").val();
    if(estateId == -1){
        layer.msg("请保存新房基础数据!");
        return false;
    }
    layer.open({
        type: 1,
        title:'户型图',
        content:$('#image_hide'),
        area: ['450px', '550px'],
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes:function(){
            if($("#newHouseKey").val() == null || $("#newHouseKey").val() == "" || $("#newHouseKey").val() == undefined){
                alert("请上传图片!");
                return;
            }
            $.ajax({
                type: "POST",
                url: "/newHouse/addNewHouseImage",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                data: {
                    id:$("#NHImageId").val(),
                    estateId: estateId,
                    imgType:$("#imgType").val(),
                    imgKey:$("#newHouseKey").val()
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success == true) {
                        queryHouseImage();
                        $("input[id='NHImageHideRest']").click();
                        newHouse_img_url.src = "";
                        $("#newHouseKey").val("");
                        $("#NHImageId").val("");
                        layer.closeAll();
                        layer.msg('操作成功!');
                    } else {
                        layer.msg({title: '错误',content: data.message});
                    }
                }
            });
        },
        btn2:function () {
            $("input[id='NHImageHideRest']").click();
            newHouse_img_url.src = "";
            $("#NHImageId").val("");
            $("#newHouseKey").val("");
        }
        ,cancel: function(){
            $("input[id='NHImageHideRest']").click();
            newHouse_img_url.src = "";
            $("#NHImageId").val("");
            $("#newHouseKey").val("");
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
}

function toNHImageUpdate(id) {
    $.ajax({
        type: "POST",
        url: "/newHouse/getImageById",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:id,
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                var obj = data.data;
                newHouse_img_url.src = obj.imgUrl;
                $("#NHImageId").val(obj.id);
                $("#newHouseKey").val(obj.imgKey);
                $("#imgType").val(obj.imgType);
                layui.use(['form'], function(){
                    form = layui.form();
                    form.render('select');
                });
                addNewHouseImage();
            } else {
                layer.msg({title: '错误',content: data.message});
            }
        }
    });
}

/**业务信息*/
function queryBusInfo() {
    var id = $("#id").val();
    if(id != "-1"){
        var url = "/newHouse/getBusInfoList";
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                estateId:id
            },
            dataType: 'json',
            success: function (data) {
                if (data.success == true) {
                    var list = data.data.list;
                    if (list != null) {
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            var obj = list[i];
                            html += "<tr>"
                                + " <td>" + obj.id + "</td>"
                                + " <td>" + obj.description + "</td>"
                                + "<td>" + obj.brokerOneName + "</td>"
                                + "<td>" + laydate.now(obj.updatedTime) + "</td>"
                                + "<td>"
                                + "<button onclick=toBusInfoUpdate("+obj.id+") class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>编辑</button>"
                                + "<button onclick=toSubDelete("+obj.id+",3) class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>删除</button>"
                                + "</td>"
                                + "</tr>";
                        }
                        $("#businfo_value").html(html);
                    }
                } else {
                    layer.open({
                        title: '错误'
                        ,content: data.message
                    });
                }
            }
        });
    }

}
/**
 * 业务信息新增更改
 * @param id
 * @returns {boolean}
 */
function addBusInfoOrUpdate() {
    var estateId=$("#id").val();
    if(estateId == -1){
        layer.msg("请保存新房基础数据!");
        return false;
    }
    layer.open({
        type:1,
        title:'结佣信息',
        content:$('#busInfo_hide'),
        area: ['450px', '520px'],
        scrollbar:false,
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes:function(){
            if($("#busInfo_desc").val() == null || $("#busInfo_desc").val() == "" || $("#busInfo_desc").val() == undefined){
                alert("结佣信息,不能为空！");
                return false;
            }else {
                if ($("#busInfo_desc").val().length >200) {
                    layer.msg("结佣信息请在200字内!");
                    return false;
                }
            }

            if($("#busDepName").val() != null && $("#busDepName").val() != "" && $("#busDepName").val() != undefined){
                if ($("#busDepName").val().length > 50) {
                    layer.msg("业务组名不能超过50字!");
                    return false;
                }
            }
            if($("#brokerOneName").val() == null || $("#brokerOneName").val() == "" || $("#brokerOneName").val() == undefined){
                alert("对接人,不能为空！");
                return false;
            }else {
                if ($("#brokerOneName").val().length >10) {
                    layer.msg("对接人请在10字内!");
                    return false;
                }
            }

            if($("#brokerOneTel").val() == null || $("#brokerOneTel").val() == "" || $("#brokerOneTel").val() == undefined){
                alert("对接人手机,不能为空！");
                return false;
            }else {
                if ($("#brokerOneTel").val().length > 11) {
                    layer.msg("对接人手机请在11字内!");
                    return false;
                }
                var reg = new RegExp("^[0-9]*$");
                if(!reg.test($("#brokerOneTel").val())){
                    alert("对接人手机,请输入数字！");;
                    return false;
                }
            }
            if($("#brokerTwoName").val() != null && $("#brokerTwoName").val() != "" && $("#brokerTwoName").val() != undefined){
                if ($("#brokerTwoName").val().length > 10) {
                    layer.msg("备用对接人请在10字内!");
                    return false;
                }

                if($("#brokerTwoTel").val() == null || $("#brokerTwoTel").val() == "" || $("#brokerTwoTel").val() == undefined){
                    alert("备用对接人手机,不能为空！");
                    return false;
                }else {
                    if ($("#brokerTwoTel").val().length > 11) {
                        layer.msg("备用对接人手机请在11字内!");
                        return false;
                    }
                    var reg = new RegExp("^[0-9]*$");
                    if(!reg.test($("#brokerTwoTel").val())){
                        alert("备用对接人手机,请输入数字！");;
                        return false;
                    }
                }
            }
            $.ajax({
                type: "POST",
                url: "/newHouse/addBusInfo",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                data: {
                    id:$("#BusInfoId").val(),
                    estateId: estateId,
                    busDepName: $("#busDepName").val(),
                    brokerOneName: $("#brokerOneName").val(),
                    brokerOneTel: $("#brokerOneTel").val(),
                    brokerTwoName: $("#brokerTwoName").val(),
                    brokerTwoTel: $("#brokerTwoTel").val(),
                    description: $("#busInfo_desc").val()
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success == true) {
                        queryBusInfo();
                        $("input[id='busInfoHideReset']").click();
                        $("#BusInfoId").val("");
                        layer.closeAll();
                        layer.msg('操作成功!');
                    } else {
                        layer.open({title: '错误',content: data.message});
                    }
                }
            });
        },
        btn2:function(){
            $("input[id='busInfoHideReset']").click();
            $("#BusInfoId").val("");
        } ,
        cancel: function(){
            $("input[id='busInfoHideReset']").click();
            $("#BusInfoId").val("");
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
}

/**
 * 更新赋值
 * @param id
 */
function toBusInfoUpdate(id) {
    $.ajax({
        type: "POST",
        url: "/newHouse/getBufInfoById",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:id,
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                var obj = data.data;
                $("#BusInfoId").val(obj.id);
                $("#busDepName").val(obj.busDepName);
                $("#brokerOneName").val(obj.brokerOneName);
                $("#brokerOneTel").val(obj.brokerOneTel);
                $("#brokerTwoName").val(obj.brokerTwoName);
                $("#brokerTwoTel").val(obj.brokerTwoTel);
                $("#busInfo_desc").val(obj.description);
                addBusInfoOrUpdate();
            } else {
                layer.msg({title: '错误',content: data.message});
            }
        }
    });
}

/**动态*/
function queryDynamicList() {
    var id = $("#id").val();
    if(id != "-1"){
        var url = "/newHouse/getDynamicList";
        $.ajax({
            type: "POST",
            url: url,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                estateId:id
            },
            dataType: 'json',
            success: function (data) {
                if (data.success == true) {
                    var list = data.data.list;
                    if (list != null) {
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            var obj = list[i];
                            html += "<tr>"
                                + " <td>" + obj.id + "</td>"
                                + " <td>" + obj.title + "</td>"
                                + "<td>" + obj.description + "</td>"
                                + "<td>" + laydate.now(obj.updatedTime) + "</td>"
                                + "<td>"
                                + "<button onclick=toDynamicUpdate("+obj.id+") class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>编辑</button>"
                                + "<button onclick=toSubDelete("+obj.id+",4) class='layui-btn layui-btn-mini'><i class='fa fa-search'></i>删除</button>"
                                + "</td>"
                                + "</tr>";
                        }
                        $("#dynamic_value").html(html);
                    }
                } else {
                    layer.open({
                        title: '错误'
                        ,content: data.message
                    });
                }
            }
        });
    }

}

/**跳转到新增页面*/
function addDynamicOrUpdate() {
    var estateId=$("#id").val();
    if(estateId == -1){
        layer.msg("请保存新房基础数据!");
        return false;
    }
    layer.open({
        type:1,
        title:"动态添加编辑",
        content: $("#dynamic_hide"),
        area: ['400px', '300px'],
        btn: ['保存','取消'],
        btnAlign: 'c',
        yes:function(){
            if($("#title").val() == null || $("#title").val() == "" || $("#title").val() == undefined){
                alert("动态标题,不能为空！");
                return;
            }else {
                if ($("#title").val().length >50) {
                    layer.msg("动态标题在50字内!");
                    return false;
                }
            }
            if($("#dy_desc").val() == null || $("#dy_desc").val() == "" || $("#dy_desc").val() == undefined){
                alert("销售状况,不能为空！");
                return;
            }else {
                if ($("#dy_desc").val().length >200) {
                    layer.msg("销售状况在200字内!");
                    return false;
                }
            }
            $.ajax({
                type: "POST",
                url: "/newHouse/addDynamic",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                data: {
                    estateId: estateId,
                    id:$("#dynamicId").val(),
                    title: $("#title").val(),
                    description: $("#dy_desc").val()
                },
                dataType: 'json',
                success: function (data) {
                    if (data.success == true) {
                        queryDynamicList();
                        $("input[id='dynamicReset']").click();
                        $("#dynamicId").val("");
                        layer.closeAll();
                        layer.msg('操作成功!');
                    } else {
                        layer.open({title: '错误',content: data.message});
                    }
                }
            });
        },btn2:function () {
            $("input[id='dynamicReset']").click();
            $("#dynamicId").val("");
        },cancel: function(){
            $("input[id='dynamicReset']").click();
            $("#dynamicId").val("");
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
}

/**
 * 更新赋值
 * @param id
 */
function toDynamicUpdate(id) {
    $.ajax({
        type: "POST",
        url: "/newHouse/getDynamicById",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:id,
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                var obj = data.data;
                $("#dynamicId").val(obj.id);
                $("#title").val(obj.title);
                $("#dy_desc").val(obj.description);
                addDynamicOrUpdate();
            } else {
                layer.msg({title: '错误',content: data.message});
            }
        }
    });
}

/**
 * 更新赋值
 * @param id
 */
function toRecommend(id,state) {
    $.ajax({
        type: "POST",
        url: "/newHouse/recommendImage",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            id:id,
            estateId:$("#id").val(),
            state:state,
        },
        dataType: 'json',
        success: function (data) {
            if (data.success == true) {
                queryHouseImage();
                layer.msg("保存成功!")
            } else {
                layer.msg(data.message);
            }
        }
    });
}

/**
 * 类型删除
 * @param id
 * @param type 1 户型 2 图片 3 业务 4 动态
 */
function toSubDelete(id,type) {
    layer.confirm('您是否确认删除此信息？', {
        btn: ['是','否']
    }, function(){
        $.ajax({
            type: "POST",
            url: "/newHouse/delete",
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                id:id,
                deleteType:type
            },
            dataType: 'json',
            success: function (data) {
                if (data.success == true) {
                    if (type==1){
                        queryLayoutImage();
                    }
                    if (type==2){
                        queryHouseImage();
                    }
                    if (type==3){
                        queryBusInfo();
                    }
                    if (type==4){
                        queryDynamicList();
                    }
                    layer.msg('操作成功!');
                } else {
                    layer.open({title: '错误',content: data.message});
                }
            }
        });
    });
}

function addMapMark() {
    var longitude = $("#areaLongitude").val();
    var latitude = $("#areaLatitude").val();
    if(longitude == null || longitude == "" || longitude == undefined || latitude == null || latitude == "" || latitude == undefined){
        longitude = 121.480237;
        latitude = 31.236305;
    }

    // 百度地图获取中心坐标点
    var map2 = new BMap.Map('allmap2',{enableMapClick:false});
    var poi2 = new BMap.Point(longitude,latitude);
    map2.centerAndZoom(poi2, 14);//16个级别 18最高 最低3
    map2.enableScrollWheelZoom();

    var marker2 = new BMap.Marker(poi2);  // 创建标注
    map2.addOverlay(marker2);               // 将标注添加到地图中
    marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
    //单击获取点击的经纬度
    map2.addEventListener("click", showInfo);
    function showInfo(e){
        $("#areaLongitude").val(e.point.lng);
        $("#areaLatitude").val(e.point.lat);
        map2.clearOverlays();
        poi2=new BMap.Point(e.point.lng,e.point.lat);
        marker2 = new BMap.Marker(poi2);  // 创建标注
        map2.addOverlay(marker2);               // 将标注添加到地图中
        marker2.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
    }
}

/**
 * 自动匹配
 */
function getDeveloperContent(obj) {
    var kw = jQuery.trim($(obj).val());
    if (kw == "") {
        $("#dcappend").hide().html("");
        return false;
    }
    var html = "";
    $.ajax({
        type: "POST",
        url: "/newHouse/developer/list",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            name:$("#developerCompany").val(),
            estateStatus:parseInt(1),
            pageSize:parseInt(10000),
            pageNum: parseInt(1)
        },
        dataType: 'json',
        success: function(data) {
            if (data.success == true) {
                var list = data.data.list;
                if (list != null) {
                    for (var i = 0; i < list.length; i++) {
                        html = html + "<div class='item' onmouseenter='getDeveloperFocus(this)' onClick=getDeveloperCon('" + list[i].name + "'," + list[i].id + ");>" + list[i].name + "</div>";
                        if (html != "") {
                            $("#dcappend").show().html(html);
                            $("#developerId").val();
                        } else {
                            $("#dcappend").hide().html("");
                        }
                    }
                }
            }
        }
    });
}

function getDeveloperFocus(obj) {
    $(".item").removeClass("addbg");
    $(obj).addClass("addbg");
}
function getDeveloperCon(name,id) {
    $("#developerCompany").val(name);
    $("#developerId").val(id);
    $("#dcappend").hide().html("");
}


function getManagerContent(obj) {
    var kw = jQuery.trim($(obj).val());
    if (kw == "") {
        $("#mappend").hide().html("");
        return false;
    }
    var html = "";
    $.ajax({
        type: "POST",
        url: "/newHouse/manager/list",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        data: {
            name:$("#mangerCompany").val(),
            estateStatus:parseInt(1),
            pageSize:parseInt(10000),
            pageNum: parseInt(1)
        },
        dataType: 'json',
        success: function(data) {
            if (data.success == true) {
                var list = data.data.list;
                if (list != null) {
                    for (var i = 0; i < list.length; i++) {
                        html = html + "<div class='item' onmouseenter='getMFocus(this)' onClick=getMCon('" + list[i].name + "'," + list[i].id + ");>" + list[i].name + "</div>";
                        if (html != "") {
                            $("#mappend").show().html(html);
                            $("#mangerId").val();
                        } else {
                            $("#mappend").hide().html("");
                        }
                    }
                }
            }
        }
    });
}

function getMFocus(obj) {
    $(".item").removeClass("addbg");
    $(obj).addClass("addbg");
}
function getMCon(name,id) {
    $("#mangerCompany").val(name);
    $("#mangerId").val(id);
    $("#mappend").hide().html("");
}

/**取消*/
function closeTab() {
    var temp = parent.navtab.getCurrentTabId();
    layer.confirm('确认取消编辑？', {
        btn: ['确认', '取消'],
        btnAlign: 'c'
    }, function () {
        parent.navtab.deleteTab(temp);
    });
}

/**
 新增物业
*/
function toMangerAdd() {
    title = '新增物业';
    href = '../newHouse/manager/toAdd';
    icon = 'larry-tianjia';
    var data = {
        href: href,
        icon: icon,
        title: title
    }
    parent.navtab.tabAdd(data);
}

/**新增开发商*/
function toDevAdd() {
    title = '新增开发商';
    href = '../newHouse/developer/toAdd';
    icon = 'larry-tianjia';
    var data = {
        href: href,
        icon: icon,
        title: title
    }
    parent.navtab.tabAdd(data);
}
